package com.example.service;

import com.example.annotation.Autowired;
import com.example.annotation.Service;
import com.example.annotation.Transactional;
import com.example.dao.AccountDao;
import com.example.pojo.Account;

/**
 * 标记@Service：向集合中注册
 * 标记@Transactional：进行动态代理，开启事务控制
 * <p>
 * 创建私有属性：accountDao，设置 "testAccount"
 * <p>
 * 根据该Id，从集合中获取指定对象注入
 *
 * @author kangshuai
 */
@Service
@Transactional
public class TransferServiceImpl implements TransferService {

    /**
     * 从容器中获取指定id的bean对象
     */
    @Autowired("testAccount")
    private AccountDao accountDao;


    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        accountDao.updateAccountByCardNo(to);
//        int c = 1 / 0;
        accountDao.updateAccountByCardNo(from);
    }
}
